

//设置html根目录字体大小
require(['jquery','../modules/pageSize'],function($,pageSetSize){
    setRootFontSize(pageSetSize);
})



/*订单页面*/
require(['jquery','swiper','../modules/popup'],function($,aswiper,popup){

    var _mdList = [];

    $('.shop-cart').each(function(i,o){
        $(this).attr('data-index',i);
        _mdList.push($(this));
    });

    function removeShopCart(i) {
        _mdList[i].remove();
        _mdList.splice(i,1);
    }

    // 订单支付方式选择
    $('.pay-method li').on('click',function(){
        $(this).addClass('active').siblings().removeClass('active');
    })




    //物流方式显示
    $('.logStatus').on('click',function(){
        if($(this).hasClass('active')){
            $(this).removeClass('active');
            $(this).siblings('.logistic-popup').hide();
        } else{
            $(this).addClass('active');
            $(this).siblings('.logistic-popup').show();
        }
    })


    // 购物车编辑状态切换
    $('.fixed-modify').on('click',function () {
        $(this).hide().siblings().show();
        $('.fixed-bot-modify').toggle();
        $('.choice-btn,.shop-choice,.all-delete,.all-choice').removeClass('active');

        $(".shop-choice").addClass("delete");
        $(".choice-btn").addClass("delete");
    })


    /*商品滑动效果*/
    $('.swipe-item').bind('swipeleft',function(){
        $(this).animate({'left':-100},200);
    })
    $('.swipe-item').bind('swiperight',function(){
        $(this).animate({'left':0},200);
    })



    /*$(function(){
         var is_no_addr = $('.address-none');
        if(is_no_addr.length == 1){

            popup.alertPopup('请先添加收货地址！');
            setTimeout(function () {
                window.location.href='/Address/index'+getFixedExtraUrl();
            },2000);
        }
    })*/


    $('.clearCart').on('click',function () {
        $_this = $(this);
        $.ajax({
            url:'/Home/Carts/clearAll'+getFixedExtraUrl(),
            dataType:'json',
            tpye:'post',
            success:function (data) {
                if(data['no_goods']){
                    window.location.reload();
                }
                if(data['clearAll']==1){
                    $_this.parents().find('.cart-content').remove();
                    $('#cart-total-value span').text(data['goods_total_fee'].toFixed(2));
                    window.location.reload();
                }
            },
            error:function () {
                popup.alertPopup('参数错误');
            }
        })


    })


    /**
     * 全选
     */
    $('.all-choice').on('click',function () {
       if($('.all-choice').hasClass('active')){
           $.ajax({
               url:'/Home/Carts/allChoice'+getFixedExtraUrl(),
               dataType:'json',
               type:'post',
               data:{'type':'remove'},
               success:function(data){
                    if(data['all_check']==0){
                        $('.all-choice').removeClass('active');
                        $('.shop-choice').removeClass('active');
                        $('.choice-btn').removeClass('active');
                        for(var i=0;data[i];i++){
                           var item = data[i];
                            var lid = item['local_id'];
                            //console.log($(".shop-cart[data-lid='"+lid+"']"));
                            //$(".shop-cart[data-lid='"+lid+"'] .ship_fee").text("运费"+data[i]['ship_fee']+"元");
                            $(".shop-cart[data-lid='"+lid+"'] .ship_fee").text("(不含运费)");
                            $(".shop-cart[data-lid='"+lid+"'] .shop-goods-price").text(data[i]['local_total'].toFixed(2));
                        }
                        $('#cart-total-value span').text(data['goods_total_fee'].toFixed(2));


                    }
               },
               error:function(){
                   popup.alertPopup('参数错误');
               }
           })


           //$('.all-choice').removeClass('active');
       }else{
           $.ajax({
               url:'/Home/Carts/allChoice'+getFixedExtraUrl(),
               dataType:'json',
               type:'post',
               data:{'type':'add'},
               success:function(data){
                   if(data['all_check']==1){
                       $('.all-choice').addClass('active');
                       $('.shop-choice').addClass('active');
                       $('.choice-btn').addClass('active');
                       for(var i=0;data[i];i++){
                           var item = data[i];
                           var lid = item['local_id'];
                           //$(".shop-cart[data-lid='"+lid+"'] .ship_fee").text("运费"+data[i]['ship_fee']+"元");
                           $(".shop-cart[data-lid='"+lid+"'] .ship_fee").text("(不含运费)");
                           $(".shop-cart[data-lid='"+lid+"'] .shop-goods-price").text(data[i]['local_total'].toFixed(2));
                       }
                       $('#cart-total-value span').text(data['goods_total_fee'].toFixed(2));

                   }
               },
               error:function(){
                   popup.alertPopup('参数错误');
               }
           })
           //$('.all-choice').addClass('active');
       }
    })



    $('.delete').on('click',function () {

        var cid=$(this).data('id');

        $_this=$(this);

        $.ajax({
            url:'/Home/Carts/isDelete'+getFixedExtraUrl(),
            type:'post',
            dataType:'json',
            data:{'cid':cid},
            success:function(data){

                if(data['no_goods']){
                     window.location.reload();
                }

                var lid=$_this.parents('.shop-cart').data('lid');
                for(var i=0;data[i];i++){
                    if(lid==data[i]['local_id']){
                        var item=data[i]['products'];
                        for(var j=0;item[j];j++){
                            if(item[j]['id']==cid){
                                popup.alertPopup('删除商品失败，网络延迟，请稍后重试');
                                return false;
                            }
                        }
                        var btn=$_this.parents('.shop-cart').find('.shop-choice');
                        (data[i]['shop_checkd'] == 1)?btn.addClass('active'):btn.removeClass('active');


                        //运费
                        //$_this.parents('.shop-cart').find('.ship_fee').text("运费"+data[i]['ship_fee']+"元");
                        $_this.parents('.shop-cart').find('.ship_fee').text("(不含运费)");
                        //店铺费用
                        $_this.parents('.shop-cart').find('.shop-goods-price').text('￥'+data[i]['local_total'].toFixed(2));
                        $('#cart-total-value span').text(data['goods_total_fee'].toFixed(2));
                        $_this.parents('.yc-data-item').remove();
                        $_this.parents('.single-outrange').remove();
                        return false;
                    }
                }
                removeShopCart($_this.parents('.shop-cart').attr('data-index'));
                $('#cart-total-value span').text(data['goods_total_fee'].toFixed(2));

            },
            error:function(){
                popup.alertPopup('删除失败，网络延迟，请稍后重试');
            }
        })
    });

    $('.collect').on('click',function () {
        var _self=$(this);
        var cid=_self.data('pid');
        var local_id = _self.data('lid');
        $.ajax({
            url:'/Home/Member/productLike'+getFixedExtraUrl(),
            type:'post',
            dataType:'json',
            data:{"product_id": cid,"local_id": local_id},
            success:function(data){
                if(data.stat){
                    //_self.toggleClass('active');
                    _global.promote.alertPopup(data.msg);
                    _self.off('click');
                }else{
                    _global.promote.alertPopup(data.msg);
                }
            },
            error:function(){
                popup.alertPopup('服务器异常，请稍后重试');
            }
        })
    });

    $('.logistic-popup').find('a').on('click',function(){
        $_this=$(this);
        if($_this.attr('key')==$_this.parent().siblings('.logStatus').attr('key')&&$_this.attr('key')==1){
            $_this.addClass('active').siblings().removeClass('active');
            var lstkey = $_this.attr('key');
            $_this.parent().siblings('.logStatus').text($_this.text()).removeClass('active').attr('key',lstkey);
            $_this.parent().hide();
            return false;
        } else if($_this.attr('key')==2){
            //alert('选自提');
            window.location.href="/Home/Address/addressSince"+getFixedExtraUrl()+"?sid="+$_this.data('local');
        } else{
            //alert('自提变送货');
            $.ajax({
                url:'/Home/Carts/changeShipping'+getFixedExtraUrl(),
                type:'post',
                data:{local_id:$_this.data('local'),shipping:$_this.attr('key')},
                dataType:'json',
                success:function (data) {
                    //console.log(data);
                    $_this.addClass('active').siblings().removeClass('active');
                    var lstkey = $_this.attr('key');
                    $_this.parent().siblings('.logStatus').text($_this.text()).removeClass('active').attr('key',lstkey);
                    $_this.parent().hide();


                    //店铺商品 不在配送区域
                    var pos = parseInt($_this.parents('.shop-cart').attr('data-index'));
                    var cData = data[pos]['products'];
                    var cDataItems = _mdList[pos].find(".yc-data-item");
                    //_mdList[pos].find('.ship_fee').text("运费"+data[pos]['ship_fee']+"元");
                    _mdList[pos].find('.ship_fee').text("(不含运费)");
                    _mdList[pos].find('.shop-goods-price').text('￥'+data[pos]['local_total'].toFixed(2));
                    $('#cart-total-value').html("￥<span>"+data['goods_total_fee']+"</span>");
                    cDataItems.each(function (i,o) {
                        if(cData[i]['active']==0){
                            if(cData[i]['group_id']){
                                $(o).attr('class','group-outrange yc-data-item');
                            }else{
                                $(o).attr('class','single-outrange yc-data-item');
                            }
                         }
                    });
                    /*$_this.parents('.shop-cart').find('.zt-address p').text('');*/

                },
                error:function (err) {
                    popup.alertPopup('服务器异常，请稍后重试');
                    window.location.reload();
                }
            })
        }

    })

    $('.plus').on("click",function(){
        $(this).attr("disabled",false);
        $(this).removeAttr("href");
        /*if($(this).siblings('.goods_number').val()>=10){
            $(this).siblings('.goods_number').val(10);
            popup.alertPopup('最大为10');
            return false;
        }*/
        var num=$(this).siblings('.goods_number').val();
        var cid=$(this).parents('.yc-data-item').data('cid');
        var lid=$(this).parents('.yc-data-item').data('lid');
        //console.log(cid);
        $_this=$(this);

        $.ajax({
            url:'/Home/Carts/changeCartsNum'+getFixedExtraUrl(),
            type:'post',
            dataType:'json',
            data:{'cid':cid,'type':'plus','num':num},
            success:function(data){
                if(data['tips']!=''){
                    popup.alertPopup(data['tips']);
                    return false;
                }
                //返回数量
                if(!data){
                    return false;
                }

                var cdata = null;
                for(var i=0;data[i];i++){
                    cdata = data[i];
                    if(lid == cdata.local_id){
                        //运费
                        //$(".shop-cart[data-lid='"+lid+"'] .ship_fee").text("运费"+cdata['ship_fee']+"元");
                        $(".shop-cart[data-lid='"+lid+"'] .ship_fee").text("(不含运费)");
                        //返回该店铺总价
                        $(".shop-cart[data-lid='"+lid+"'] .shop-goods-price").text("￥"+cdata['local_total'].toFixed(2));
                        var pos = -1;
                        var _num = 0;
                        for(var j =0;j < cdata.products.length;j++){
                            var cproduct = cdata.products[j];
                            if(cproduct["id"] == cid){
                                pos = j;
                                _num = cproduct["cart_num"];
                                break;
                            }
                        }
                        if(pos != -1){
                            $_this.siblings('.goods_number').val(_num);
                        }
                        break;
                    }
                }
                $(".public-cart-number").text(data.num);
                $("#cart-total-value span").text(data.goods_total_fee.toFixed(2));
                $(this).attr("disabled",true);
                $(this).attr("href",'javascript:void(0)');
            },
            error:function(){
                $(this).attr("disabled",true);
                $(this).attr("href",'javascript:void(0)');
                popup.alertPopup('数量更改失败,请检查网络，稍后重试');
            }
        })
    })
    $('.minus').on("click",function(){
        $(this).attr("disabled",false);
        $(this).removeAttr("href");
        //console.log('-');
        if($(this).siblings('.goods_number').val()<=1){
            $(this).siblings('.goods_number').val(1);
            popup.alertPopup('最小为1');
            return false;
        }
        var num=$(this).siblings('.goods_number').val();
        var cid=$(this).parents('.yc-data-item').data('cid');
        var lid=$(this).parents('.yc-data-item').data('lid');
        //console.log(cid);
        $_this=$(this);
        $.ajax({
            url:'/Home/Carts/changeCartsNum'+getFixedExtraUrl(),
            type:'post',
            dataType:'json',
            data:{'cid':cid,'type':'minus','num':num},
            success:function(data){
                //console.log(data);
                if(!data){
                    return false;
                }
                for(var i=0;data[i];i++){
                    //console.log(data[i]);
                    if(lid!=data[i].local_id){
                        continue;
                    }

                    var item=data[i];
                    for(var j=0;item['products'][j];j++) {
                        var item1=item['products'][j];
                        //console.log(item1);
                        if(item1.id!=cid){
                            //console.log(item1);
                            continue;
                        }
                        //返回该店铺总价
                        var shop_price = $_this.parents('.shop-cart').find('.shop-goods-price');
                        shop_price.text("￥"+data[i]['local_total'].toFixed(2));
                        //运费
                        //$_this.parents('.shop-cart').find('.ship_fee').text("运费"+data[i]['ship_fee']+"元");
                        $_this.parents('.shop-cart').find('.ship_fee').text("(不含运费)");
                        $_this.siblings('.goods_number').val(item1['cart_num']);
                        break;
                    };
                }
                $(".public-cart-number").text(data.num);
                $("#cart-total-value span").text(data.goods_total_fee.toFixed(2));
                $(this).attr("disabled",true);
                $(this).attr("href",'javascript:void(0)');
            },
            error:function(){
                $(this).attr("disabled",true);
                $(this).attr("href",'javascript:void(0)');
                popup.alertPopup('数量更改失败，请检查网络，稍后重试');
            }

        })
    })

    $('.choice-btn').on('click',function(){

        if($(this).hasClass('active')){
            var cid=$(this).parents('.yc-data-item').data('cid');

            $_this=$(this);
            $.ajax({
                url:'/Home/Carts/changeIsChecked'+getFixedExtraUrl(),
                type:'post',
                dataType:'json',
                data:{'cid':cid,'type':'remove'},
                success:function(data){
                    for(var i=0;data[i];i++){
                        var item=data[i];
                        //console.log(item['local_id']+'/'+$_this.parents('.yc-data-item').data('lid'));
                        if(item['local_id']!=$_this.parents('.yc-data-item').data('lid')){
                            continue;
                        }
                        for(var j=0;item['products'][j];j++){
                            var item1=item['products'][j];
                            //console.log(cid+'/'+item1['id']);
                            if(cid!=item1['id']){
                                continue;
                            }
                            if(item1['ischeck']==0){
                                //返回该店铺总价
                                var shop_price = $_this.parents('.shop-cart').find('.shop-goods-price');
                                shop_price.text("￥"+data[i]['local_total'].toFixed(2));
                                //运费
                                //$_this.parents('.shop-cart').find('.ship_fee').text("运费"+data[i]['ship_fee']+"元");
                                $_this.parents('.shop-cart').find('.ship_fee').text("(不含运费)");
                                $_this.removeClass('active');

                                break;
                            }
                        }
                        if(item['shop_checkd']==0){
                            $_this.parents('.shop-cart').find('.shop-choice').removeClass('active');
                        }


                    }
                    $("#cart-total-value span").text(data.goods_total_fee.toFixed(2));
                    (data['all_check']==1)?$('.all-choice').addClass('active'):$('.all-choice').removeClass('active');



                },
                error:function(){
                    popup.alertPopup('请检查网络，稍后重试');
                }
            })

        }else{
            var cid=$(this).parents('.yc-data-item').data('cid');
            $_this=$(this);
            $.ajax({
                url:'/Home/Carts/changeIsChecked'+getFixedExtraUrl(),
                type:'post',
                dataType:'json',
                data:{'cid':cid,'type':'add'},
                success:function(data){
                    for(var i=0;data[i];i++){
                        var item=data[i];
                        //console.log(item['local_id']+'/'+$_this.parents('.yc-data-item').data('lid'));
                        if(item['local_id']!=$_this.parents('.yc-data-item').data('lid')){
                            continue;
                        }
                        for(var j=0;item['products'][j];j++){
                            var item1=item['products'][j];
                            //console.log(cid+'/'+item1['id']);
                            if(cid!=item1['id']){
                                continue;
                            }
                            if(item1['ischeck']==1){
                                //返回该店铺总价
                                var shop_price = $_this.parents('.shop-cart').find('.shop-goods-price');
                                shop_price.text("￥"+data[i]['local_total'].toFixed(2));
                                //运费
                                //$_this.parents('.shop-cart').find('.ship_fee').text("运费"+data[i]['ship_fee']+"元");
                                $_this.parents('.shop-cart').find('.ship_fee').text("(不含运费)");
                                $_this.addClass('active');

                                break;
                            }
                        }
                        if(item['shop_checkd']==1){
                            $_this.parents('.shop-cart').find('.shop-choice').addClass('active');
                        }
                    }
                    $("#cart-total-value span").text(data.goods_total_fee.toFixed(2));
                    (data['all_check']==1)?$('.all-choice').addClass('active'):$('.all-choice').removeClass('active');
                },
                error:function(){
                    popup.alertPopup('请检查网络，稍后重试');

                }
            })
        }
    })

    $('.shop-choice').on('click',function(){
        $_this=$(this);
        var lid=$(this).data('lid');
        if($(this).hasClass('active')){
            //$(this).removeClass('active');
            $.ajax({
                url:'/Home/Carts/localChooseAll'+getFixedExtraUrl(),
                type:'post',
                dataType:'json',
                data:{'lid':lid,'type':'remove'},
                success:function(data){
                    //console.log(data);
                    for(var i=0;data[i];i++){
                        var item=data[i];
                        if(lid!=item['local_id']){
                            continue;
                        }
                        for(var j=0;item['products'][j];j++){
                            var item1=item['products'][j];
                            var cid=item1['id'];
                            //console.log($('.yc-data-item[data-cid='+'"'+cid+'"'+']'));
                            //$('.yc-data-item[data-cid='+cid+']').removeClass('active');
                            $('.yc-data-item[data-cid='+'"'+cid+'"'+'] .choice-btn').removeClass('active');
                        }
                        //运费
                        //$_this.parents('.shop-cart').find('.ship_fee').text("运费"+data[i]['ship_fee']+"元");
                        $_this.parents('.shop-cart').find('.ship_fee').text("(不含运费)");
                        //返回该店铺总价
                        $_this.parents('.shop-cart').find('.shop-goods-price').text('￥'+data[i]['local_total'].toFixed(2));
                    }

                    $("#cart-total-value span").text(data.goods_total_fee.toFixed(2));
                    (data['all_check']==1)?$('.all-choice').addClass('active'):$('.all-choice').removeClass('active');



                    $_this.removeClass('active');
                },
                error:function(){
                    popup.alertPopup('请检查网络，稍后重试');
                }
            })
        }else{
            //$(this).addClass('active');
            $.ajax({
                url:'/Home/Carts/localChooseAll'+getFixedExtraUrl(),
                type:'post',
                dataType:'json',
                data:{'lid':lid,'type':'add'},
                success:function(data){
                    //console.log(data);
                    for(var i=0;data[i];i++){
                        var item=data[i];
                        if(lid!=item['local_id']){
                            continue;
                        }
                        for(var j=0;item['products'][j];j++){
                            var item1=item['products'][j];
                            var cid=item1['id'];
                            //console.log($('.yc-data-item[data-cid='+'"'+cid+'"'+']'));
                            //$('.yc-data-item[data-cid='+cid+']').removeClass('active');
                            $('.yc-data-item[data-cid='+'"'+cid+'"'+'] .choice-btn').addClass('active');
                        }
                        //运费
                        //$_this.parents('.shop-cart').find('.ship_fee').text("运费"+data[i]['ship_fee']+"元");
                        $_this.parents('.shop-cart').find('.ship_fee').text("(不含运费)");
                        //返回该店铺总价
                        $_this.parents('.shop-cart').find('.shop-goods-price').text('￥'+data[i]['local_total'].toFixed(2));
                    }
                    $("#cart-total-value span").text(data.goods_total_fee.toFixed(2));
                    (data['all_check']==1)?$('.all-choice').addClass('active'):$('.all-choice').removeClass('active');
                    $_this.addClass('active');
                },
                error:function(){
                    popup.alertPopup('请检查网络，稍后重试');
                }
            })
        }

    })
    //结算
    $('.settment-btn').on('click',function () {
        $.ajax({
            url:'/Home/Carts/storeIsOk'+getFixedExtraUrl(),
            dataType:'json',
            success:function(data){
                //console.log(data);
                if(data['no_address']){
                    popup.alertPopup("请填写收货地址！");
                    return false;
                }
                if(data['no_goods']){
                    popup.alertPopup("请先选择商品！");
                    return false;
                }
                if(data['is_no_check']==0){
                    popup.alertPopup("请先勾选要购买的商品！");
                    return false;
                }
                if(data['store_ok']==1){
                    window.location.href = '/Home/Orders/checkOut'+getFixedExtraUrl();
                }else {
                    popup.alertPopup(data['tips']);
                }
            },
            error:function(){
                popup.alertPopup('请检查网络，稍后重试');
            }
        })

    })





})



